package com.sky.controller.user;


import com.sky.context.BaseContext;
import com.sky.dto.BlogDTO;
import com.sky.dto.BlogPageQueryDTO;
import com.sky.entity.Blog;
import com.sky.result.PageResult;
import com.sky.result.Result;
import com.sky.service.BlogService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

@RestController
@RequestMapping("/user/blog")
@Slf4j
@Api(tags = "博客相关接口")
public class BlogController {

    @Autowired
    private BlogService blogService;

    /**
     * 分页查询论坛
     * @param blogPageQueryDTO
     * @return
     */
    @ApiOperation("分页查询")
    @GetMapping("/detail")
    public Result<PageResult> page(BlogPageQueryDTO blogPageQueryDTO){
        log.info("开始分页查询博客：{}",blogPageQueryDTO);
        PageResult pageResult = blogService.pageQuery(blogPageQueryDTO);
        return Result.success(pageResult);
    }

    /**
     * 浏览论坛增加浏览量
     * @param id
     * @return
     */
    @GetMapping("/browse/{id}")
    public Result<Blog> browseBlog(@PathVariable Long id){
        log.info("浏览论坛：{}",id);
        Blog blog=blogService.getById(id);
        blogService.addPageView(id);
        return Result.success(blog);
    }

    /**
     * 根据用户id查询论坛
     * @param blogPageQueryDTO
     * @return
     */
    @GetMapping("/byuserid")
    public Result<PageResult> pageGetByUserId(BlogPageQueryDTO blogPageQueryDTO){
        Long currentId = BaseContext.getCurrentId();
        blogPageQueryDTO.setCreateUser(currentId);
        log.info("根据用户id查询论坛：{}",blogPageQueryDTO);
        PageResult pageResult = blogService.getByUserId(blogPageQueryDTO);
        return Result.success(pageResult);
    }

    /**
     * 新增博客
     * @param blogDTO
     * @return
     */
    @PostMapping
    @ApiOperation("新增博客")
    public Result saveBlog(@RequestBody BlogDTO blogDTO){
        log.info("新增博客：{}",blogDTO);
        blogService.save(blogDTO);
        return Result.success();
    }

    /**
     * 增加点赞数
     */
    @PutMapping("/like")
    @ApiOperation("增加点赞")
    public Result likeBlog(@RequestBody BlogDTO blogDTO){
        blogDTO.setUserId(BaseContext.getCurrentId());
        blogService.addLikeCount(blogDTO);
        return Result.success();
    }

    /**
     * 取消点赞数
     */
    @PutMapping("/likedelete")
    @ApiOperation("增加点赞")
    public Result likeDeleteBlog(@RequestBody BlogDTO blogDTO){
        blogDTO.setUserId(BaseContext.getCurrentId());
        blogService.deleteLikeCount(blogDTO);
        return Result.success();
    }



}
